
import net.coobird.thumbnailator.Thumbnails;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import javax.imageio.ImageIO;

public class RemoteServer {
    private static final int PORT = 12345;
    private static Robot robot;
    
    public static void main(String[] args) {
        try {
            robot = new Robot();
            ServerSocket serverSocket = new ServerSocket(PORT);
            System.out.println("服务器已启动，等待客户端连接...");
            
            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("客户端已连接: " + socket.getInetAddress());
                
                // 启动屏幕捕获线程
                new Thread(() -> handleScreenCapture(socket)).start();
                
                // 启动输入控制线程
                new Thread(() -> handleInputControl(socket)).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static void handleScreenCapture(Socket socket) {
        try {
            OutputStream out = socket.getOutputStream();
            Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            int maxWidth = screenRect.width;
            int maxHeight = screenRect.height;
            double quality = 0.8;
            while (!socket.isClosed()) {
                BufferedImage screen = robot.createScreenCapture(screenRect);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                // 使用Thumbnailator进行智能尺寸压缩
                Thumbnails.of(screen)
                        .size(maxWidth, maxHeight)  // 根据网络状况动态设置
                        .outputQuality(quality)     // 0.6-0.8为推荐值
                        .outputFormat("jpg")
                        .toOutputStream(baos);
//                ImageIO.write(screen, "jpg", baos);

                byte[] imageBytes = baos.toByteArray();

                DataOutputStream dos = new DataOutputStream(out);
                dos.writeInt(imageBytes.length);
                dos.write(imageBytes);
                dos.flush();
                
                Thread.sleep(100); // 控制帧率
            }
        } catch (Exception e) {
            System.out.println("屏幕捕获线程终止");
        }
    }
    
    private static void handleInputControl(Socket socket) {
        try {
            InputStream in = socket.getInputStream();
            DataInputStream dis = new DataInputStream(in);
            
            while (!socket.isClosed()) {
                int eventType = dis.readInt();
                
                switch (eventType) {
                    case 1: // 鼠标移动
                        int x = dis.readInt();
                        int y = dis.readInt();
                        robot.mouseMove(x, y);
                        break;
                        
                    case 2: // 鼠标点击
                        int button = dis.readInt();
                        robot.mousePress(button);
                        robot.mouseRelease(button);
                        break;
                        
                    case 3: // 键盘输入
                        int keyCode = dis.readInt();
                        robot.keyPress(keyCode);
                        robot.keyRelease(keyCode);
                        break;
                }
            }
        } catch (Exception e) {
            System.out.println("输入控制线程终止");
        }
    }
}
